const users = require("../sql/user");
const tool = require("../sql/mongoTool");
const uuid = require("uuid");
tool.use(users)

exports.register = function () {
    return (req, res, next) => {
        tool.find({ username: req.body.username }, data => {
            if (data.length < 1) {
                users.insertMany({
                    userid: "user-" + uuid.v1(),
                    username: req.body.username,
                    password: req.body.password,
                    isLogin: 0,
                    regTime: Date.now(),
                }, () => {
                    res.send(JSON.stringify({
                        code: 1,
                        msg: "注册成功"
                    }));
                }, err => {
                    res.send(JSON.stringify({
                        code: 2,
                        msg: "注册失败，数据库出错",
                        err
                    }));
                })
            } else {
                res.send(JSON.stringify({
                    code: 0,
                    msg: "注册失败，用户名重复"
                }));
            }
        })
    };
}